<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="72px">
      <el-form-item label="工单编码" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入工单编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入工单名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="派单人" prop="dispatchName">
        <el-input
          v-model="queryParams.dispatchName"
          placeholder="请输入派单人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ruoyi-work-order:workorder:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ruoyi-work-order:workorder:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ruoyi-work-order:workorder:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ruoyi-work-order:workorder:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="workorderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="工单编码" align="center" prop="id"/>
      <el-table-column label="工单名称" align="center" prop="name"/>
      <el-table-column label="工单内容" align="center" prop="content"/>
      <el-table-column label="是否派单" align="center" prop="isDispatch">
        <template slot-scope="scope">
          {{ scope.row.isDispatch == true ? '已派单' : '未派单' }}
        </template>
      </el-table-column>
      <el-table-column label="派单人姓名" align="center" prop="dispatchName"/>
      <el-table-column label="是否回复" align="center" prop="isReply">
        <template slot-scope="scope">
          {{ scope.row.isReply == true ? '已回复' : '未回复' }}
        </template>
      </el-table-column>
      <el-table-column label="回复内容" align="center" prop="replyContent"/>
      <el-table-column label="回复图片" align="center" prop="replyImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.replyImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <!--      <el-table-column label="创建人" align="center" prop="createUser"/>-->
      <!--      <el-table-column label="修改人" align="center" prop="updateUser"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleWorkOrder(scope.row)"
          >
            {{ loginUserName == "admin" ? "派单" : "" }}
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleReply(scope.row)"
          >回复
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ruoyi-work-order:workorder:edit']"
          >{{ loginUserName == "admin" ? "修改" : "" }}
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ruoyi-work-order:workorder:remove']"
          >{{ loginUserName == "admin" ? "删除" : "" }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工单编号" prop="id">
          <el-input v-model="form.id" readonly="true"/>
        </el-form-item>
        <el-form-item label="工单名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入工单名称"/>
        </el-form-item>
        <el-form-item label="工单内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
<!--        <el-form-item label="是否派单" prop="isDispatch">-->
<!--          <el-input v-model="form.isDispatch" placeholder="请输入是否派单"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="派单人id" prop="dispatchUserId">-->
<!--          <el-input v-model="form.dispatchUserId" placeholder="请输入派单人id"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="派单人姓名" prop="dispatchName">-->
<!--          <el-input v-model="form.dispatchName" placeholder="请输入派单人姓名"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="是否回复" prop="isReply">-->
<!--          <el-input v-model="form.isReply" placeholder="请输入是否回复"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="回复内容">-->
<!--          <editor v-model="form.replyContent" :min-height="192"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="回复图片" prop="replyImage">-->
<!--          <image-upload v-model="form.replyImage"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="创建人" prop="createUser">-->
<!--          <el-input v-model="form.createUser" placeholder="请输入创建人"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="修改人" prop="updateUser">-->
<!--          <el-input v-model="form.updateUser" placeholder="请输入修改人"/>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 派单对话框 -->
    <el-dialog :title="title" :visible.sync="orderOpen" width="500px" append-to-body>
      <el-form :inline="true" :model="orderForm" class="demo-form-inline">
        <el-form-item label="工单编号" prop="id">
          <el-input v-model="orderForm.id" readonly="true"/>
        </el-form-item>
        <el-form-item label="派单">
          <el-select
            v-model="orderForm.userName"
            placeholder="请选择接单人">
            <el-option
              v-for="item in receivingOrdersOptions"
              :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="receivingOrder">派单</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 派单对话框 -->
    <el-dialog :title="title" :visible.sync="openReply" width="500px" append-to-body>
      <el-form ref="form" :model="formReply" :rules="rules" label-width="80px">
        <el-form-item label="工单编号" prop="id">
          <el-input v-model="formReply.id" readonly="true"/>
        </el-form-item>
        <el-form-item label="工单名称" prop="name">
          <el-input v-model="formReply.name" readonly="true"/>
        </el-form-item>
        <el-form-item label="工单内容">
          <editor v-model="formReply.content" :min-height="192" readonly="true" />
        </el-form-item>
        <el-form-item label="回复内容">
          <editor v-model="formReply.replyContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="回复图片" prop="replyImage">
          <image-upload v-model="formReply.replyImage"/>
        </el-form-item>
      </el-form>
      <div align="center" class="dialog-footer">
        <el-button @click="submitReply" type="primary">提 交 回 复</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listWorkorder,
  getWorkorder,
  delWorkorder,
  addWorkorder,
  updateWorkorder
} from "@/api/ruoyi-work-order/workorder";
import axios from "axios";

export default {
  name: "Workorder",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工单管理表格数据
      workorderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        name: null,
        dispatchName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "工单名称不能为空", trigger: "blur"}
        ],
        content: [
          {required: true, message: "工单内容不能为空", trigger: "blur"}
        ],
      },
      //接单人信息
      receivingOrdersOptions: [
        {
          value: 'HTML'
        },
        {
          value: 'CSS'
        },
        {
          value: 'JavaScript'
        }
      ],
      //显示弹出派单表单
      orderOpen: false,
      //派单表单参数
      orderForm: {},
      //登录用户的用户名
      loginUserName: "",
      //派单对话框
      openReply:false,
      //派单表单参数
      formReply:{}
    };
  },
  created() {
    this.getList();
    this.getLoginUserName()
  },
  methods: {
    /** 查询工单管理列表 */
    getList() {
      this.loading = true;
      listWorkorder(this.queryParams).then(response => {
        this.workorderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        content: null,
        isDispatch: null,
        dispatchUserId: null,
        dispatchName: null,
        isReply: null,
        replyContent: null,
        replyImage: null,
        createTime: null,
        updateTime: null,
        createUser: null,
        updateUser: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工单管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWorkorder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工单管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWorkorder(this.form).then(response => {
              this.$modal.msgSuccess("编辑成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWorkorder(this.form).then(response => {
              this.$modal.msgSuccess("添加成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除工单管理编号为"' + ids + '"的数据项？').then(function () {
        return delWorkorder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ruoyi-work-order/workorder/export', {
        ...this.queryParams
      }, `workorder_${new Date().getTime()}.xlsx`)
    },
    /** 点击派单按钮，弹出选择派单人的表单 */
    handleWorkOrder(row) {
      axios.get('/dev-api/system/user/getAllUserName').then(response => {
        if (response.data.code == 200) {
          this.receivingOrdersOptions = response.data.data
        } else {
          this.$modal.msgSuccess(response.data.msg);
        }
      })
      this.orderOpen = true
      this.orderForm.id = row.id
    },
    /** 提交派单信息 */
    receivingOrder() {
      axios.get('/dev-api/ruoyi-work-order/workorder/receivingOrder/' + this.orderForm.id + '/' + this.orderForm.userName).then(response => {
        if (response.data.code == 200) {
          this.$modal.msgSuccess("派单成功");
        } else {
          this.$modal.msgError(response.data.msg);
        }
        this.orderOpen = false;
        this.getList();
        this.orderForm.userName = '';
      })
    },
    /** 获取当前登录的用户名 */
    getLoginUserName() {
      axios.get("/dev-api/ruoyi-work-order/workorder/getUserName").then(response => {
        this.loginUserName = response.data.msg
      })
    },
    /** 点击回复按钮 */
    handleReply(row){
      this.openReply = true;
      this.formReply = row
    },
    /** 点击提交回复按钮 */
    submitReply(){
      axios.post("/dev-api/ruoyi-work-order/workorder/submitReply",this.formReply).then(response => {
          if(response.data.code == 200){
            this.$modal.msgSuccess("回复成功");
          }else {
            this.$modal.msgError(response.data.msg);
          }
          this.openReply = false;
          this.getList()
      })
    }
  }
};
</script>
